#===============================================
# Code written by Joe France (joeprogram.com)
# Released under code.google.com/p/video-scholar/
# with the MIT license (aka, the do-whatever-you-want-with-it license)
#===============================================

import pyglet
from AbstractGUI import AbstractGUI

class GUI( pyglet.sprite.Sprite, AbstractGUI ):
	'''
	A GUI knows how to draw itself
	And how to handle things like being clicked on
	'''
	
	HIGHLIGHT_EFFECT = 80 # The number to add the color to give a highlight effect
	
	image = pyglet.resource.image( 'GUI.png' )
	
	def __init__( self, image = None, origin = "left bottom", color = (255,255,255), **kwargs ):
	
		# We allow the class to set its own image
		# But can explicitly overwrite it with a parameter
		self.image = pyglet.resource.image( image ) if image else self.image
		
		super( GUI, self).__init__( self.image, **kwargs )
		
		self.base_color = color
		self.color = color
		
		# Handle the origin
		# This allows us to have GUIs automatically handle their offset
		# And greatly cleans the math
		origin_parts = origin.split(' ')
		if origin_parts[0] == 'right':
			self.image.anchor_x = self.image.get_image_data().width
		elif origin_parts[0] == 'center':
			self.image.anchor_x = self.image.get_image_data().width/2
		else:
			if origin_parts[0] != 'left':
				print "WARNING: Received invalid origin argument %s. SOLUTION: Please use 'left', 'right', or 'center'." % origin
			self.image.anchor_x = 0
		
		if origin_parts[1] == 'top':
			self.image.anchor_y = self.image.get_image_data().height
		elif origin_parts[1] == 'center':
			self.image.anchor_y = self.image.get_image_data().height/2
		else:
			if origin_parts[1] != 'bottom':
				print "WARNING: Received invalid origin argument %s. SOLUTION: Please use 'top','bottom',or 'center'." % origin
			self.image.anchor_y = 0
		
	def set_coordinates( self, x = None, y = None ):
		'''
		Set the coordinates of the GUI
		Allows for None to be passed in for x or y
		in case you don't wish to change one of the variables
		'''
		
		if x is not None:
			self.x = x
		if y is not None:
			self.y = y
			
	def get_coordinates( self ):
		return (self.x, self.y)
		
	def get_image( self ):
		return self.image
		
	def get_height( self ):
		return self.height
		
	def get_width( self ):
		return self.width
		
	def highlight( self ):
		'''
		Highlights the GUI
		Helpful for mouse states with buttons and the like
		'''
		
		# White can't get any brighter,
		# So we make it darker instead
		if self.base_color == (255,255,255):
			self.color = (230, 230, 230 )
		else:
			self.color = map( lambda x: min( x + self.HIGHLIGHT_EFFECT ,255), self.base_color )
		
	def dehighlight( self ):
		'''
		Call this to revert the effects of a highlight
		'''
		self.color = self.base_color
		
	def hit_test( self, x, y ):
		'''
		Returns true if the provided x and y coordinates intersect with this GUI
		'''
		return x >= self.x - self.image.anchor_x and x <= self.x + self.width - self.image.anchor_x and y >= self.y - self.image.anchor_y and y <= self.y + self.height - self.image.anchor_y
		
	def mouse_over( self, x, y ):
		self.highlight()
		
	def mouse_out( self, x, y ):
		self.dehighlight()
		
	def mouse_press( self, x, y, button, modifiers ):
		pass		